﻿'   VBA Extractor - exports VB source code from macro enabled Word and Excel documents
'   Copyright (c) 2013 Tony Jollans & Neil Dodson

'   This program is free software; you can redistribute it and/or
'   modify it under the terms of the GNU General Public License
'   as published by the Free Software Foundation; either version 2
'   of the License, or (at your option) any later version.

'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.

'   You should have received a copy of the GNU General Public License
'   along with this program; if not, write to the Free Software
'   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


Imports System.IO

Public Class frmMain

    Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        txtDebug.Text = My.Settings.CopyrightText
        Try
            txtSourceFile.Text = My.Settings.SourceFile
            txtDestinationFolder.Text = My.Settings.DestinationFolder
            sourceFileDialogue.InitialDirectory = txtSourceFile.Text
            destinationFolderDialogue.RootFolder = txtDestinationFolder.Text
        Catch
        End Try
        TryEnableExecuteButton()
    End Sub


    Private Sub btnChangeSource_Click(sender As System.Object, e As System.EventArgs) Handles btnChangeSource.Click
        If sourceFileDialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtSourceFile.Text = sourceFileDialogue.FileName
        End If
    End Sub
     
    Private Sub btnChangeDestination_Click(sender As System.Object, e As System.EventArgs) Handles btnChangeDestination.Click
        If destinationFolderDialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtDestinationFolder.Text = destinationFolderDialogue.SelectedPath
        End If
    End Sub

    Private Sub txtSourceFile_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSourceFile.TextChanged
        TryEnableExecuteButton()
    End Sub

    Private Sub txtDestinationFolder_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtDestinationFolder.TextChanged
        TryEnableExecuteButton()
    End Sub

    Private Sub TryEnableExecuteButton()
        Dim enabled As Boolean = False
        Dim sourceFile As String = txtSourceFile.Text
        Dim destinationFolder As String = txtDestinationFolder.Text

        If sourceFile <> "" And File.Exists(sourceFile) Then
            My.Settings.SourceFile = sourceFile
        Else
            My.Settings.SourceFile = ""
        End If


        If destinationFolder <> "" Then
            If Directory.Exists(destinationFolder) Then
                My.Settings.DestinationFolder = destinationFolder
            End If
        End If

        btnExecute.Enabled = False
        If Len(My.Settings.DestinationFolder) > 0 Then
            If Len(My.Settings.SourceFile) > 0 Then
                btnExecute.Enabled = True
            End If
        End If

    End Sub



    Private Sub btnExecute_Click(sender As System.Object, e As System.EventArgs) Handles btnExecute.Click
        Try
            txtDebug.Text = ""

            btnExecute.Enabled = False
            Dim sourceFile As String = txtSourceFile.Text
            Dim destinationFolder As String = txtDestinationFolder.Text

            If String.IsNullOrWhiteSpace(sourceFile) Or String.IsNullOrWhiteSpace(destinationFolder) Then Return

            If Not Directory.Exists(destinationFolder) Then
                Try
                    Directory.CreateDirectory(destinationFolder)
                    My.Settings.DestinationFolder = destinationFolder
                Catch ex As Exception
                    My.Settings.DestinationFolder = ""
                    Throw
                End Try
            End If

            Dim parser As New OleParser.Extractor(sourceFile)
            AddHandler parser.Message, Sub(o, args) txtDebug.AppendText(args.Message & vbCrLf)

            For Each codeFile In parser.Execute()
                Dim filePath As String = Path.Combine(destinationFolder, codeFile.FileName & codeFile.FileExtension)
                txtDebug.AppendText(String.Format("Writing file {0}{1}", filePath, vbCrLf))
                File.WriteAllText(filePath, codeFile.Code)
            Next codeFile

        Catch ex As Exception
            OnError(ex.Message)
        Finally
            btnExecute.Enabled = True
        End Try
    End Sub


    Private Sub OnError(ByVal Message As String)
        txtDebug.AppendText(Message & vbCrLf)
        MessageBox.Show(Message)
    End Sub
End Class
